<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/kendocommon.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>教师信息维护</title>
    <meta charset="utf-8">
    <link href="${basepath}/css/viewcss/main/workschedule/scheduleform.css" rel="stylesheet" type="text/css"/>
    <style>
    	table{font-size:12px}
    	.btndiv{
			text-align:center;
		}
		.btn{
			display: inline-block; 
			width: 80px;
			height: 40px;
			border-radius:4px;
			border: 1px solid #CCC;
			background: #F5F5F5;
			padding-top: 10px;
		}
		.querybtn{
			text-align:center;
			display: inline-block; 
			width: 60px;
			height: 18px;
			border-radius:4px;
			border: 1px solid #CCC;
			background: #F5F5F5;
			padding-top: 4px;
		}
    </style>
    <script>
    	
		//加载部门
		var loadDept = function(){
			//$("#ownerDeptID option").remove();
			$.ajax({
		        url: '${path}/commonSel/queryDepartmentPark',
		        dataType: 'json',
		        data:{dept_id:'${sessionScope.shopoauser.user_shopdept_id}'},
		        success: function(doc) {
		            $.each(doc,function(k,v) {
		            	$("#ownerDeptID").append("<option value='"+v.id+"'>"+v.text+"</option>");
		            });
		        }
		    });
		}
		//加载用户
		var loadUser = function(deptId,userId){
			$("#ownerID option").remove();
			$.ajax({
		        url: '${path}/commonSel/queryUserByDeptId',
		        dataType: 'json',
		        data:{dept_id:deptId},
		        success: function(doc) {
		            $.each(doc,function(k,v) {
		            	$("#ownerID").append("<option value='"+v.id+"'>"+v.text+"</option>");
		            	//console.log(v);
		            });
		            if(userId){
		            	$("#ownerID").val(userId);
		            }
		        }
		    });
		}
    
        $(document).ready(function() {
        	
        	$(".various").fancybox({
        		title       : "操作历史",
        		maxWidth	: 1000,
        		maxHeight	: 700,
        		fitToView	: false,
        		width		: '60%',
        		height		: '30%',
        		autoSize	: false,
        		closeClick	: false,
        		openEffect	: 'none',
        		closeEffect	: 'none'
        	});
        	
        	loadDept();
    		$('#ownerDeptID').change(function(){
    			  loadUser($(this).val());
    		});
        	
        	$("#toolbar").kendoToolBar({
	            resizable: false,
	            items: [
	            	{ type: "separator" },
	                //{ template: toolbarTemp},
					//{ type: "separator" },
	                //{ template: '<label>旗舰店:</label><input id="park" class="k-textbox"/>'},
	                //{ type: "separator" },
	                { type: "button", text: "刷新",click: queryInfoList }
	            ]
	        });
	        
            dataSource = new kendo.data.DataSource({
                transport: {
                    read:  {
                        url: "${path}/task/queryTaskList",
                        dataType: "json"
                    }
                },
                //group: [{field: "plan_month_detailName"}],
                serverPaging: true,
                serverFiltering: true,
                pageSize: 15,
                schema: {
                	data:"data",
               		total:"total"
                }
            });
            
            $("#grid").kendoGrid({
                dataSource: dataSource,
                //pageable: true,
                pageable: {
					refresh: true,
					pageSizes: true,
					buttonCount: 5,
					page: 1,
					pageSize: 10,
					pageSizes: [10, 20, 30],
					messages: {
						display: "显示 {0}-{1} 共 {2} 项",
						empty: "没有数据",
						itemsPerPage: "每面显示数量",
						first: "第一页",
						last: "最后一页",
						next: "下一页",
						previous: "上一页"
					}
				},
                selectable: "multiple row",
                allowCopy: true,
                //groupable: {
                //    messages: {
                //      empty: "将列拖拽到此处进行分类查看"
                //    }
                //},
                //editable: 'incell',
                columns: [
					//{ field:"checked", template: "<input type='checkbox' class='gridchk' />",width: "40px",
					//	headerTemplate: '<input type="checkbox" id="check-all"/>'},
					{locked: true, command: [
								{name:"inforow",text:"<span class='k-icon k-edit'></span>查看",click:searchInfo,buttonType:"ImageAndText"},
					            {name:"editrow",text:"<span class='k-icon k-edit'></span>任务分配",click:queryInfo,buttonType:"ImageAndText"},
					            {name:"receiverow",text:"<span class='k-icon k-add'></span>接收",click:receiveTask,buttonType:"ImageAndText"},
					            {name:"refuserow",text:"<span class='k-icon k-cancel'></span>拒绝",click:refuseTask,buttonType:"ImageAndText"},
					            {name:"refreshrow",text:"<span class='k-icon k-redo'></span>重新下发",click:reSendTask,buttonType:"ImageAndText"},
					            {name:"deleterow",text:"<span class='k-icon k-delete'></span>删除",click:deleteTask,buttonType:"ImageAndText"}
					            ], title: "操作", width: "480px" },
					//{ field:"opterbtn", title: "#",width: "100px",template:btnTemplate},//,template:checksex	
                	{ locked: true,field:"title", title: "任务标题",width: "120px",template:distributeTemp},
                	{ field:"parentTaskName", title: "父级任务",width: "120px"},
                    { field:"plan_month_detailName", title: "对应月季划",width: "120px"},//,template:checksex
                    { field:"beginTime", title: "开始时间",width: "120px"},
                    { field:"endTime", title: "结束时间",width: "120px"},
                    { field:"needTime", title: "所需时间",width: "80px"},
                    //{ field:"standard", title: "标准",width: "80px"},
                    { field:"priority_typeCode", title: "优先级",width: "120px",template:priorityTemp},
                    { field:"frequency", title: "变更频率",width: "120px"},
                    { field:"completeRatio", title: "完成百分比",width: "120px"},
                    { field:"task_statusCode", title: "任务状态",width: "120px",template:taskStatusTemp},
                    { field:"acceptStatusCode", title: "接收状态",width: "120px",template:acceptStatusTemp}
                    //{ field: "UnitsInStock", title:"Units In Stock", width:"120px"},//,format:"{0:yyyy/MM/dd}"
                ],
                dataBound: function (e) {
                    var grid = e.sender;
                    var dataItems = grid.dataSource.data();
                    $.each(grid.items(), function (i, item) {
                        var curDataItem = dataItems[i];
                        if(curDataItem){
                        	//父任务显示分配按钮，子任务隐去分配按钮
                        	if(curDataItem.acceptStatusCode!=1||curDataItem.applyUserID != '${sessionScope.shopoauser.oa_user_id}'){
                        		$(item).find(".k-grid-editrow").remove();
                        	}else{
                        		if (curDataItem.parentId != null ||curDataItem.applyUserID != '${sessionScope.shopoauser.oa_user_id}') {
	                        		$(item).find(".k-grid-editrow").remove();
	                            }
                        	}
                        	//接收状态为接收状态判断删除按钮是否显示    如果不是接受状态，则自己创建的任务自己才有权删除
                            if(curDataItem.acceptStatusCode==1||curDataItem.applyUserID != '${sessionScope.shopoauser.oa_user_id}'){
                            	$(item).find(".k-grid-deleterow").remove();
                            }
                            //拒绝的任务才可重新下发  自己创建的任务自己才可重新下发
							if(curDataItem.applyUserID=='${sessionScope.shopoauser.oa_user_id}'){
								if (curDataItem.acceptStatusCode!=2) {
									$(item).find(".k-grid-refreshrow").remove();
								}
							}else{
								$(item).find(".k-grid-refreshrow").remove();
							}

                            //接收状态为不为初始状态判断接收与拒绝按钮是否显示
                            if(curDataItem.acceptStatusCode!=0){
                            	$(item).find(".k-grid-receiverow").remove();
                            	$(item).find(".k-grid-refuserow").remove();
                            }
                        }
                    });
                }
            });
            $('#grid').css("height",document.body.clientHeight-38);
		});
        
        var distributeTemp = function(dataItem){
			return "<a href='${path}/task/distribute?id="+dataItem.id+"' style='color:#000' class='various' data-fancybox-type='iframe'>" + dataItem.title + "</a>";
		}
        
		var priorityTemp = function(dataItem){
			var status = "";
			switch(dataItem.priority_typeCode)
			{
				case "1":status="A";break;
				case "2":status="B";break;
				case "3":status="C";break;
				case "4":status="D";break;
			}
			return status;
		}
		
		var taskStatusTemp = function(dataItem){
			var status = "";
			switch(dataItem.task_statusCode){
				case "0":status="完成";break;
				case "1":status="过期";break;
				case "2":status="已变更";break;
				case "3":status="暂停";break;
				case "4":status="正常";break;
			}
			return status;
		}
		
		var acceptStatusTemp = function(dataItem){
			var status = "";
			switch(dataItem.acceptStatusCode)
			{
				case "0":status="未接收";break;
				case "1":status="已接收";break;
				case "2":status="已拒绝";break;
			}
			return status;
		}
		
		var searchInfo = function(e){//任务查看
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            if(data.acceptStatusCode==0){
            	return layer.msg("请先接收任务", {time: 3000, icon:6});
            }
            clearForm();
            setForm(data);
            showInfo();
            $('#saveBtn').css("display","none");
		}
		
		var queryInfo = function(e){//任务分配
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            if(data.acceptStatusCode==0){
            	return layer.msg("请先接收任务", {time: 3000, icon:6});
            }
            clearForm();
            setForm(data);
            showInfo();
            $('#saveBtn').css("display","block");
		}
		
		//接受任务
		var receiveTask = function(e){//任务分配
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            layer.confirm('接收后状态无法改变，确定接收?', function(index){
            	data.acceptStatusCode="1";
           		updateTask(data);
            });
		}
		
		//拒绝任务
		var refuseTask = function(e){//任务分配
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            layer.prompt({
           	    formType: 2,
           	    //value: '请输入原因',
           	    title: '拒绝接收'
           	}, function(value, index, elem){
           		data.refuseReason = value;
           		data.acceptStatusCode="2";
           		//console.log(data);
				updateTask(data);
           	});
		}
		
		//重新下发
		var reSendTask = function(e){//任务分配
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            layer.prompt({
           	    formType: 2,
           	    //value: '请输入原因',
           	    title: '重新下发'
           	}, function(value, index, elem){
           		data.refuseReason = value;
           		data.acceptStatusCode="0";
           		//console.log(data);
				updateTask(data);
           	});
		}
		
		var updateTask = function(data){
			$.ajax({
		        type:"POST", 
		        url: "${path}/task/updateTask",
		        data: JSON.stringify(data),
		        dataType: 'json',
   				contentType: "application/json;charset=utf-8",
		        success: function (text) {
		        	$("#grid").data("kendoGrid").dataSource.fetch();
		        	layer.msg(text.msg, {time: 3000, icon:6});
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	layer.msg("与远程服务器交互异常，请规范操作或重新登录", {time: 3000, icon:6});
		        }
		    });
		}
		
		//删除自己维护的任务
		var deleteTask = function(e){//任务分配
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
			var delTask = {id:data.id};
			layer.confirm('是否删除?', function(index){
				$.ajax({
			        type:"POST", 
			        url: "${path}/task/deleteTask",
			        data: JSON.stringify(delTask),
			        dataType: 'json',
	   				contentType: "application/json;charset=utf-8",
			        success: function (text) {
			        	$("#grid").data("kendoGrid").dataSource.fetch();
			        	layer.msg(text.msg, {time: 3000, icon:6});
			        	layer.close(layIndex);
			        },
			        error: function (jqXHR, textStatus, errorThrown) {
			        	layer.msg("与远程服务器交互异常，请规范操作或重新登录", {time: 3000, icon:6});
			        }
			    });
			})
		}
		
		var queryInfoList = function(e){//查询
			$("#grid").data("kendoGrid").dataSource.fetch();
		}
		
		var layIndex;
		var showInfo = function(){
			layIndex = layer.open({
				title:"任务分派",
				type: 1,
				fix: false,
		        //shadeClose: true,
		        maxmin: true,
				area: ['750px', '550px'], //宽高
			    content: $('#inlineForm'),
			    cancel: function(index){
			    	$("#grid").data("kendoGrid").dataSource.fetch();
		        }
			});
		}
		
		var clearForm = function(){
			$('#ownerDeptID').val('');
			$('#id').val('');
			$('#plan_month_detailID').val('');
			$('#plan_typeID').val('');
			$('#parentId').val('');
			$('#title').val('');
			$('#priority_typeCode').val('');
			$('#beginTime').val('');
			$('#endTime').val('');
			$('#needTime').val('');
			$('#frequency').val('');
			$('#goal').val('');
			$('#standard').val('');
			$('#plan_month').html('');
			$('#completeRatio').val('');
			$('#otherInfo').html('');
		}
		
		var setForm = function(data){
			//loadDept();
			$('#ownerDeptID').val(data.ownerDeptID);
			loadUser(data.ownerDeptID,data.ownerID);
			//$('#ownerID').val(data.ownerID);
			//$('#id').val(data.id);
			$('#plan_month_detailID').val(data.plan_month_detailID);
			$('#plan_typeID').val(data.plan_typeID);
			$('#parentId').val(data.id);
			$('#title').val(data.title);
			$('#priority_typeCode').val(data.priority_typeCode);
			$('#beginTime').val(data.beginTime);
			$('#endTime').val(data.endTime);
			$('#needTime').val(data.needTime);
			$('#frequency').val(data.frequency);
			$('#goal').val(data.goal);
			$('#standard').val(data.standard);
			$('#plan_month').html("计划类型:"+data.planTypeName+",参考月季划:"+data.plan_month_detailName);
			$('#completeRatio').val(data.completeRatio);
			$('#otherInfo').html(data.marketingPlanOrEvaluation);
		}
		
		//保存增子任务
		var saveTask = function(e){
			var formval = JsonCode(taskForm);
			formval = JSON.parse(formval);
			formval.ownerName=$('#ownerID').find("option:selected").text();
			formval.ownerDeptName=$('#ownerDeptID').find("option:selected").text();
			//formval = JSON.stringify(formval);
			//console.log(formval);
			
			$.ajax({
		        type:"POST", 
		        url: "${path}/task/addChildrenTask",
		        data: JSON.stringify(formval),
		        dataType: 'json',
   				contentType: "application/json;charset=utf-8",
		        success: function (text) {
		        	$("#grid").data("kendoGrid").dataSource.fetch();
		        	layer.msg(text.msg, {time: 3000, icon:6});
		        	if(text.status==1){
		        		layer.close(layIndex);
		        	}
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	layer.msg("与远程服务器交互异常，请规范操作或重新登录", {time: 3000, icon:6});
		        }
		    });
		}
		
    </script>
</head>
<body>
	<div id="toolbar" style="margin:2px"></div>
    <div id="grid" style="margin:2px;font-size:10px"></div>
    
    
    <div id="inlineForm" style="display:none;padding:30px">
		<form class="bootstrap-frm" action="" method="post" id="taskForm" name="taskForm">
			<input type="hidden" name="id" id="id" >
			<input type="hidden" name="plan_month_detailID" id="plan_month_detailID" >
			<input type="hidden" name="parentId" id="parentId" >
			<input type="hidden" name="plan_typeID" id="plan_typeID" >
			<h1 id="plan_month" style="text-align:center"></h1>
			<table>
                <tr>
                    <td style="padding:5px">
                        <label style="font-size:14px;">标题</label>
                    </td>
                    <td style="padding:5px">
                    	<input type="text" placeholder="标题" name="title" id="title" style="width:200px">
                    </td>
                    <td style="padding:5px">
                        <label style="font-size:14px;">优先级</label>
                    </td>
                    <td style="padding:5px">
                    	<select name="priority_typeCode" id="priority_typeCode" style="width:200px">
                    		<option value=''>请选择</option>
                    		<option value='1'>A</option>
                    		<option value='2'>B</option>
                    		<option value='3'>C</option>
                    		<option value='4'>D</option>
                    	</select>
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px">
                       <label style="font-size:14px;">责任部门</label>
                    </td>
                    <td style="padding:5px">
                    	<select name="ownerDeptID" id="ownerDeptID" style="width:200px">
                    		<option value=''>请选择</option>
                    	</select>
                    </td>
                    <td style="padding:5px">
                        <label style="font-size:14px;">责任人</label>
                    </td>
                    <td style="padding:5px">
                    	<select name="ownerID" id="ownerID" style="width:200px">
                    		<option value=''>请选择</option>
                    	</select>
                    </td>
                </tr>
                <tr>
                    <td style="padding:5px">
                       <label style="font-size:14px;">开始日期</label>
                    </td>
                    <td style="padding:5px">
                    	<input type="text" name="beginTime" id="beginTime" class="laydate-icon" 
                    		onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm'})" style="width:200px">
                    </td>
                    
                    <td style="padding:5px">
                        <label style="font-size:14px;">结束时间</label>
                    </td>
                    <td style="padding:5px">
                    	<input type="text" name="endTime" id="endTime" class="laydate-icon" 
                    		onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm'})" style="width:200px">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;padding:5px">
                        <label style="font-size:14px;">所需时间</label>
                    </td>
                    <td style="width:220px;padding:5px">
                    	<input type="text" placeholder="所需时间" name="needTime" id="needTime" style="width:200px">
                    </td>
                    <td style="padding:5px">
                        <label style="font-size:14px;">变更频率</label>
                    </td>
                    <td style="padding:5px">
                    	<input type="text" placeholder="变更频率" name="frequency" id="frequency" style="width:200px">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;padding:5px">
                        <label style="font-size:14px;">完成百分比</label>
                    </td>
                    <td colspan="3" style="width:220px;padding:5px">
                    	<input type="text" placeholder="完成百分比" name="completeRatio" id="completeRatio" style="width:200px">
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;padding:5px">
                        <label style="font-size:14px;">任务目标</label>
                    </td>
                    <td colspan="3" style="padding:5px">
                        <textarea name="goal" id="goal" style="width:490px;height:50px"></textarea>
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;padding:5px">
                        <label style="font-size:14px;">任务标准</label>
                    </td>
                    <td colspan="3" style="padding:5px">
                        <textarea name="standard" id="standard" style="width:490px;height:50px"></textarea>
                    </td>
                </tr>
            </table>
            <div class="btndiv" id="saveBtn"><a href="#" onclick="saveTask()" class="btn">确定</a></div>
            <h1 style="text-align:center;text-align:left">附加信息</h1>
            <div id="otherInfo"></div>
        </form>
	</div>
</body>
</html>
